package com.tienan.batterychecker.widget;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.IBinder;
import android.widget.ProgressBar;
import android.widget.RemoteViews;
import android.widget.TextView;

import com.tienan.batterychecker.R;
import com.tienan.batterychecker.battery.BatteryCheck;

public class WidgetService extends Service {

	Context mContext;
	int appWidgetId;
	static int levelBattery, statusCharging;

	static TextView txtLevelBattery;
	ProgressBar mProgressBarBattery;

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		appWidgetId = -1;
		levelBattery = 0;
		registerReceiver(batteryInfoReceiver, new IntentFilter(
				Intent.ACTION_BATTERY_CHANGED));
		registerReceiver(batteryInfoReceiver, new IntentFilter(
				Intent.ACTION_POWER_CONNECTED));
		registerReceiver(batteryInfoReceiver, new IntentFilter(
				Intent.ACTION_POWER_DISCONNECTED));
		return super.onStartCommand(intent, flags, startId);
	}

	private static BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
				levelBattery = intent
						.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
				statusCharging = intent.getIntExtra(
						BatteryManager.EXTRA_STATUS, -1);
				updateViews(context);
			} else if (action.equals(Intent.ACTION_POWER_CONNECTED)) {
				levelBattery = intent
						.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
				statusCharging = intent.getIntExtra(
						BatteryManager.EXTRA_STATUS, -1);
				updateViews(context);
			} else if (action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
				levelBattery = intent
						.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
				statusCharging = intent.getIntExtra(
						BatteryManager.EXTRA_STATUS, -1);
				updateViews(context);
			}
		}
	};

	private static void updateViews(Context context) {
		RemoteViews view = new RemoteViews(context.getPackageName(),
				R.layout.widget_activity);
		try {
			view.setTextViewText(R.id.levelBattery, levelBattery + "%");
			view.setProgressBar(R.id.progressBarBattery, 100, levelBattery,
					false);
			if (statusCharging == BatteryManager.BATTERY_STATUS_CHARGING) {
				view.setImageViewResource(R.id.imgBattery,
						R.drawable.ic_health_landscape_charging);
			} else {
				view.setImageViewResource(R.id.imgBattery,
						R.drawable.ic_health_landscape);
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		// click
		try {
			Intent configIntent = new Intent(context, BatteryCheck.class);
			PendingIntent pending = PendingIntent.getActivity(context, 0,
					configIntent, PendingIntent.FLAG_UPDATE_CURRENT);
			view.setOnClickPendingIntent(R.id.lnBattery, pending);
		} catch (Exception e) {
			// TODO: handle exception
		}

		ComponentName componentName = new ComponentName(context,
				WidgetProvider.class);
		AppWidgetManager appWidgetManager = AppWidgetManager
				.getInstance(context);
		appWidgetManager.updateAppWidget(componentName, view);
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

}
